package com.resource.unit
{
	import com.resource.animation.IAnimation;
	
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	/**
	 * 一个单位的各个部件组合，包括武器，如果做换装，则包括头，身体，手脚等，支持换装
	 * 可能会有动作同步问题
	 */
	public class BaseUnit extends Sprite implements IAnimation
	{
		public static const NUDE:String = "_nude";
		public static const LEFT_ARM:String = "_leftArm";
		public static const LEFT_LEG:String = "_leftLeg";
		public static const BODY:String = "_body";
		public static const RIDE:String = "_ride";
		public static const HAT:String = "_hat";
		public static const WEAPON:String = "_weapon";
		public static const RIGHT_LEG:String = "_rightLeg";
		public static const RIGHT_ARM:String = "_rightArm";
		
		public static const PART_ARR:Array = ["_nude", "_leftArm", "_leftLeg", "_body", "_ride", "_hat", "_weapon", "_rightLeg", "_rightArm"];
		
		private var _nude:ActionUnit;
		private var _leftArm:ActionUnit;
		private var _leftLeg:ActionUnit;
		private var _body:ActionUnit;
		private var _ride:ActionUnit;
		private var _hat:ActionUnit;
		private var _weapon:ActionUnit;
		private var _rightLeg:ActionUnit;
		private var _rightArm:ActionUnit;
		
		private var _dir:int = 1;
		private var _act:String;
		private var _currentFrame:int = 0; // 用来同步所有部件的播放帧
		
		private var _endCallback:Function;
		private var _isStop:Boolean = false;
		
		public function BaseUnit()
		{
			super();	
		}
	
		public function enterFrame():void{
			if(_isStop){
				return;
			}
			for(var i:int=0; i<PART_ARR.length; i++){
				var part:ActionUnit = this[PART_ARR[i]];
				if(part){
					part.enterFrame();
				}
			}
			_currentFrame++;
		}
		
		/**
		 * resUrl 是目录路径
		 * loadComplete 预加载资源并回调
		 */
		public function setPart(name:String, resUrl:String, loadComplete:Function=null):void{
			var part:ActionUnit = this[name];
			if(part && part.resUrl == resUrl){
				if(loadComplete){
					loadComplete.apply();
				}
				return;
			}else{
				part = new ActionUnit;
				this[name] = part;
				part.resUrl = resUrl;
				part.dir = _dir;
				part.act = _act;
				part.getAnimation().gotoAndPlay(_currentFrame); // 同步帧
				
				var p:ActionUnit;
				for(var i:int=0; i<PART_ARR.length; i++){
					if(name != PART_ARR[i]){
						if(this[PART_ARR[i]]){
							p = this[PART_ARR[i]];
						}
					}else{
						break;
					}
				}
				if(p){
					addChildAt(part, getChildIndex(p) + 1);
				}else{
					addChildAt(part, 0);
				}
			}
			if(name == NUDE){
				part.endCallback = _endCallback;
			}
			
			if(loadComplete){
				part.preLoad(loadComplete);
			}
		}
		
		public function set endCallback(value:Function):void{
			_endCallback = value;
			if(_nude){
				_nude.endCallback = _endCallback;
			}
		}
		
		public function get endCallback():Function{
			return _endCallback;
		}
		
		public function get act():String
		{
			return _act;
		}
		
		public function set act(value:String):void
		{
			_act = value;
			for(var i:int=0; i<PART_ARR.length; i++){
				var part:ActionUnit = this[PART_ARR[i]];
				if(part){
					part.act = _act;
				}
			}
			// 这里所有部件都重新开始播放
			_currentFrame = 0;
		}
		
		public function get dir():int
		{
			return _dir;
		}
		
		public function set dir(value:int):void
		{
			if(_dir != value){
				_dir = value;
				for(var i:int=0; i<PART_ARR.length; i++){
					var part:ActionUnit = this[PART_ARR[i]];
					if(part){
						part.dir = _dir;
					}
				}
			}
		}
		
		public function getAttRect():Object{
			return _nude.getCurrentAct().attRect;
		}
		
		public function getHitRect():Object{
			return _nude.getCurrentAct().hitRect;
		}
		
		public function getSendBullet():Object{
			return _nude.getCurrentAct().sendBullet;
		}
		
		public function getPosition():Point{
			return _nude.getCurrentAct().position;
		}
		
		public function set isStop(value:Boolean):void{
			_isStop = value;
		}
		
		public function get isStop():Boolean{
			return _isStop;
		}
	}
}